package com.ctrip.cap.monitor.screenshot;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;

import com.ctrip.cap.device.android.AndroidDevice;
import com.ctrip.cap.monitor.common.DeviceManager.DeviceConnectStateListener;
import com.ctrip.cap.monitor.utils.LogUtil;

public class ScreenShot implements DeviceConnectStateListener {

	private static final Map<String, ScreenShotTask> sTask = new HashMap<String, ScreenShotTask>();
	private static final ExecutorService mTaskService = Executors
			.newCachedThreadPool();
	
	public ScreenShot(){
		
		
	}
	@Override
	public void onDisconnected(String sn) {
		synchronized (sTask) {
			ScreenShotTask task=sTask.get(sn);
			if (task != null) {
				task.stop();
			}
		}
	}

	@Override
	public void onConnected(AndroidDevice device, AtomicBoolean isStart) {
		synchronized (sTask) {
			if (device != null && !sTask.containsKey(device.getSerialNumber())) {
				ScreenShotTask task = new ScreenShotTask(device, isStart);
				mTaskService.execute(task);
				LogUtil.info(device.getSerialNumber()+":  ScreenShotTask  excute!");
				sTask.put(device.getSerialNumber(), task);
			}
		}
	}

	public void setDeviceEnable(String sn, boolean flag) {
		ScreenShotTask task = sTask.get(sn);
		if (task != null) {
			task.setStart(flag);
			LogUtil.info("ScreenShotTask  start!");
		}
	}

}
